﻿using UnityEngine;

public class PlayerHealth : MonoBehaviour
{
    [Header("生命")] [SerializeField] private int health = 100;
    [SerializeField] private int healthMax = 100;
    public bool isDead;

    private void Start()
    {
        RxEventBus.Publish(new PlayerHpChangeEvent(health, healthMax));
    }

    public void TakeDamage(int damage)
    {
        // TODO 计算最终伤害值
        health -= damage;
        if (health <= 0)
        {
            Dead();
        }

        RxEventBus.Publish(new PlayerHpChangeEvent(health, healthMax));
    }

    private void Dead()
    {
        isDead = true;
        health = 0;
        RxEventBus.Publish(new GameOverEvent());
    }
}